#pragma once
#include "KompexSQLiteDatabase.h"
#include "KompexSQLiteStatement.h"

#include <string>
#include <vector>
#include <map>

namespace LittleBase{
	#define SQLITE_OPEN_READONLY         0x00000001  /* Ok for sqlite3_open_v2() */
	#define SQLITE_OPEN_READWRITE        0x00000002  /* Ok for sqlite3_open_v2() */
	#define SQLITE_OPEN_CREATE           0x00000004  /* Ok for sqlite3_open_v2() */
	#define SQLITE_OPEN_DELETEONCLOSE    0x00000008  /* VFS only */
	#define SQLITE_OPEN_EXCLUSIVE        0x00000010  /* VFS only */
	#define SQLITE_OPEN_AUTOPROXY        0x00000020  /* VFS only */
	#define SQLITE_OPEN_URI              0x00000040  /* Ok for sqlite3_open_v2() */
	#define SQLITE_OPEN_MEMORY           0x00000080  /* Ok for sqlite3_open_v2() */
	#define SQLITE_OPEN_MAIN_DB          0x00000100  /* VFS only */
	#define SQLITE_OPEN_TEMP_DB          0x00000200  /* VFS only */
	#define SQLITE_OPEN_TRANSIENT_DB     0x00000400  /* VFS only */
	#define SQLITE_OPEN_MAIN_JOURNAL     0x00000800  /* VFS only */
	#define SQLITE_OPEN_TEMP_JOURNAL     0x00001000  /* VFS only */
	#define SQLITE_OPEN_SUBJOURNAL       0x00002000  /* VFS only */
	#define SQLITE_OPEN_MASTER_JOURNAL   0x00004000  /* VFS only */
	#define SQLITE_OPEN_NOMUTEX          0x00008000  /* Ok for sqlite3_open_v2() */
	#define SQLITE_OPEN_FULLMUTEX        0x00010000  /* Ok for sqlite3_open_v2() */
	#define SQLITE_OPEN_SHAREDCACHE      0x00020000  /* Ok for sqlite3_open_v2() */
	#define SQLITE_OPEN_PRIVATECACHE     0x00040000  /* Ok for sqlite3_open_v2() */
	#define SQLITE_OPEN_WAL              0x00080000  /* VFS only */


	class SQLiteHaper
	{
	public:
		SQLiteHaper();
		~SQLiteHaper();

		bool OpenDateBase(std::string strDateBase, int flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
		bool OpenRecordset(std::string strSQL);
		int	 GetRecCount();
		void CloseRecordset();

		bool Get_EOF();

		int         GetFieldValueAsInt(std::string strFieldName, int iDefault = 0);
		std::string GetFieldValueAsInt2(std::string strFieldName, int iDefault = 0);
		std::string GetFieldValueAsStr(std::string strFieldName, std::string strDefault = "");
		double      GetFieldValueAsDouble(std::string strFieldName, double dDefault = 0.0);
		std::string GetFieldValueAsDouble2(std::string strFieldName, int dDefault = 0.0);
		std::string GetFieldValueAutoStr(std::string strFieldName, std::string strDefault = "");


		int  GetVersionNumber();

		bool Execute(std::string strSQL);		
		bool Execute(std::vector<std::string> vectSQL);


		bool BeginTransaction();
		bool ExecuteTransactionSQL(std::string strSQL);
		long CommitTransaction();


	protected:
		bool isOpenDateBase;
		bool isOpenRecordset;
		bool isBeginTramsaction;
		Kompex::SQLiteDatabase *pDatabase;
		Kompex::SQLiteStatement *pStatement;
		std::map<std::string, std::string> CurrMapColumn;
		std::map<std::string, int> CurrMapColumn2Nums;

		template <class Type> Type StringToNum(const std::string& str);
		template <class Type> std::string NumToString(const Type& str);

		void to_gb2312(std::string& strutf8);
		void to_utf8(std::string& strAnsic);
	};

	class TestModel_SQLiteHaper{
	public:
		TestModel_SQLiteHaper();
		~TestModel_SQLiteHaper();
	};

}


